package com.handpay.zztong.hp.main.model.component;

/**
 * Created by lyang on 2017/4/10.
 */


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;


public class ComponentRelationItemHandler extends DefaultHandler {
  private ComponentRelationItemBean mComponentRelationItem;
    private   ArrayList<ComponentRelationItemBean> mComponentRelationItemList;

    final int PARENT_ITEM_CODE = 1;
    final int ITEM_CODE = 2;
    final int ORDER_NUM = 3;
    int currentstate = 0;


    public ComponentRelationItemHandler() {
    }


    @Override
    public void startDocument() throws SAXException {
        mComponentRelationItemList = new ArrayList<ComponentRelationItemBean>();
    }

    public ArrayList<ComponentRelationItemBean> getComponentRelationList() {
        return mComponentRelationItemList;
    }

    @Override
    public void endDocument() throws SAXException {

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (localName.equals("item")) {
            mComponentRelationItem = new ComponentRelationItemBean();
            return;
        }


        if (localName.equals("PARENT_ITEM_CODE")) {
            currentstate = PARENT_ITEM_CODE;
            return;
        }
        if (localName.equals("ITEM_CODE")) {
            currentstate = ITEM_CODE;
            return;
        }
        if (localName.equals("ORDER_NUM")) {
            currentstate = ORDER_NUM;
            return;
        }

        currentstate = 0;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("item")) {
            mComponentRelationItemList.add(mComponentRelationItem);
            return;
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String theString = new String(ch, start, length);
        switch (currentstate) {
            case PARENT_ITEM_CODE:
                mComponentRelationItem.setParentItemCode(theString);
                currentstate = 0;
                break;
            case ITEM_CODE:
                mComponentRelationItem.setItemCode(theString);
                currentstate = 0;
                break;
            case ORDER_NUM:
                mComponentRelationItem.setOrderNum(theString);
                currentstate = 0;
                break;
            default:
                return;
        }
    }
}